<template>
  <el-dialog
    title="角色选择"
    :visible.sync="showRoles"
    :before-close="handleRoles"
  >
    <el-checkbox-group v-model="getcheckList">
      <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="handleRoles">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getroleList, RoleListById } from '@/api/setting'
import { getDetailInfoById } from '@/api/user'
export default {
  props: {
    showRoles: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      getcheckList: [],
      checkList: [],
      total: ''
    }
  },
  created() {
    this.getroleList()
  },
  methods: {
    // 获取所有角色数据
    async getroleList() {
      const { total } = await getroleList()
      this.total = total
      const { rows } = await getroleList({ page: 1, pagesize: this.total })
      this.checkList = rows
    },
    // 根据id获取角色
    async roleListById(id) {
      const { roleIds } = await getDetailInfoById(id)
      this.getcheckList = [...roleIds]
    },
    // 点击取消
    handleRoles() {
      this.$emit('update:showRoles', false)
      this.getcheckList = []
    },
    async btnOK() {
      await RoleListById({ id: this.roleId, roleIds: this.getcheckList })
      this.$message.success('操作成功')
      this.$emit('update:showRoles', false)
    }
  }
}
</script>

<style>

</style>
